package com.llyb.tingshu.user.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.llyb.tingshu.album.client.TrackInfoFeignClient;
import com.llyb.tingshu.common.rabbit.constant.MqConst;
import com.llyb.tingshu.common.rabbit.service.RabbitService;
import com.llyb.tingshu.common.result.Result;
import com.llyb.tingshu.model.album.TrackInfo;
import com.llyb.tingshu.model.user.UserPaidTrack;
import com.llyb.tingshu.user.mapper.UserPaidTrackMapper;
import com.llyb.tingshu.user.strategy.ItemTypeStrategy;
import com.llyb.tingshu.vo.user.UserPaidRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("1002")
public class TrackStrategy implements ItemTypeStrategy {

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private RabbitService rabbitService;

    @Qualifier("com.llyb.tingshu.album.client.TrackInfoFeignClient")
    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    /**
     * 保存付费记录
     *
     * @param userPaidRecordVo
     */
    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        // 防止添加重复数据
        LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId());
        wrapper.in(UserPaidTrack::getTrackId, itemIdList);
        List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(wrapper);
        // 找出所有存在的
        Set<Long> paidTrackIdSet = userPaidTrackList.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
        if (!CollectionUtils.isEmpty(userPaidTrackList)){
            // 找出所有存在的，利用mq增加余额
            ArrayList<Long> roolbackTrackIdList = new ArrayList<>();
            for (Long trackId : itemIdList) {
                if (paidTrackIdSet.contains(trackId)) {
                    roolbackTrackIdList.add(trackId);
                }
            }
            // 发送消息，恢复余额
            // todo 恢复余额的监听队列
            rabbitService.sendMessage(MqConst.EXCHANGE_ORDER, MqConst.ROUTING_ACCOUNT_COMPENSATION, roolbackTrackIdList);
        }
        // 获取声音对象，为了获取专辑id
        Result<TrackInfo> trackInfoR = trackInfoFeignClient.getTrackInfo(itemIdList.get(0));
        Assert.notNull(trackInfoR, "获取声音信息失败");
        TrackInfo trackInfo = trackInfoR.getData();
        // 声音
        for (Long trackId : itemIdList) {
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setTrackId(trackId);
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrackMapper.insert(userPaidTrack);
        }
    }
}
